<template>
  <div v-loading.fullscreen.lock="fullscreenLoading">
    <span class="text-right">
      <el-button size="mini" type="default" @click="uploadSplitTax"
        ><span :data-clipboard-text="waybillNo" style="color:#468e70">
          上传税单拆分文件</span
        ></el-button
      >
      <el-button size="mini" type="default" @click="generalTax"
        ><span :data-clipboard-text="waybillNo" style="color:#468e70">
          生成税单</span
        ></el-button
      >
    </span>

    <el-dialog
      title="上传税单拆分文件"
      :visible.sync="centerDialogVisible"
      width="50%"
      center
    >
      <el-card>
        <el-upload
          class="upload-demo"
          ref="upload"
          drag
          multiple
          action="fakeaction"
          :show-file-list="true"
          :http-request="uploadFn"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :auto-upload="true"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div slot="tip" class="el-upload__tip"></div>
        </el-upload>
        <!-- <el-button
          style="margin-left: 10px;"
          size="small"
          type="success"
          v-loading.fullscreen.lock="fullscreenLoading"
          @click="saveWaybillFile"
          >确定
        </el-button> -->
      </el-card>
      <el-card>
        <el-row v-if="errorTips">
          <el-col
            :span="8"
            style="height:30px;"
            v-for="(item, k) in errorTips"
            :key="k"
          >
            <em style="color:red"> {{ k }} {{ errorTips[k] }} </em>
          </el-col>
        </el-row>
      </el-card>
    </el-dialog>

    <el-dialog
      title="生成税单"
      :visible.sync="generalDialogVisible"
      width="50%"
      center
    >
      <el-card> </el-card>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import * as constant from "@/store/yg/common/constant";
import { ajax, actionPermission } from "@/store/yg/common/utils";
import { m } from "@/store/yg/common/local";
export default {
  data() {
    return {
      generalDialogVisible: false,
      errorTips: {},
      fullscreenLoading: false,
      centerDialogVisible: false,
      waybillNo: "",
      arr: [],
      fileUrls: [],
      fileNames: [],
      action: "",
      feeData: [],
      fileList: [
        {
          name: "food.jpeg",
          url: ""
        },
        {
          name: "food2.jpeg",
          url: ""
        }
      ]
    };
  },

  methods: {
    m(val) {
      return m(val);
    },

    uploadSplitTax() {
      this.centerDialogVisible = true;
    },

    uploadFn(params) {
      this.action =
        constant.API_SERVER +
        "/api/consume/upload?uri=/waybill/japansplittax/list&f=uploadSplitTaxFile&type=main";
      const file = params.file,
        fileType = file.type,
        isImage = fileType.indexOf("pdf") !== -1,
        isLt4M = file.size / 1024 / 1024 < 4;

      const form = new FormData();
      // 文件对象
      form.append("items[]", file);
      // form.append("fileName", file.name);

      this.fullscreenLoading = true;
      axios({
        method: "post",
        url: this.action,
        data: form
      })
        .then(response => {
          this.fullscreenLoading = false;
          this.errorTips = response.data.data;
          if (response.data.code === 200) {
            this.$message.success("上传税单拆分成功!");
            if (response.data.data) {
              this.fileUrls.push(response.data.data["fileUrl"]);
            }
            setTimeout(() => {
              window.location.reload(true);
            }, 3000);
          } else {
            this.$notify.error({
              title: "错误",
              message: "上传失败: "
            });
          }
        })
        .catch(error => {
          console.log(error);
          this.$notify.error({
            title: "错误",
            message: "上传失败!"
          });
        });
    },

    generalTax() {
      var params = {};
      console.log(this.$store.state.main.selectedRows);
      if (
        !this.$store.state.main.selectedData ||
        this.$store.state.main.selectedData.length <= 0
      ) {
        this.$message.error("至少选择一条数据");
        return;
      }
      var params = {};
      this.fullscreenLoading = true;
      params.ids = this.$store.state.main.selectedRows;
      ajax("generateTaxBill", params).then(
        res => {
          this.fullscreenLoading = false;
          if (res.code === 200) {
            this.$message.success("生成成功");

            // setTimeout(() => {
            //   window.location.reload(true);
            // }, 3000);
          }
        },
        error => {
          console.log(error);
          this.$message.error("生成失败");
        }
      );
    },
    submitUpload() {
      this.$refs.upload.submit();
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    }
  },
  mounted() {}
};
</script>

<style></style>
